using System;
using System.Xml;
using System.Collections; // For IEnumerable and IEnumerator
using System.ComponentModel; // For ICustomTypeDescriptor, PropertyDescriptor


	public class ColumnStructure : PropertyDescriptor {
		private string name;

		public ColumnStructure(string name) : base(name, null) {
			this.name = name;
		}

		public override bool IsReadOnly {
			get {
				return true;
			}
		}

		public override void SetValue(object component, object value) {
		}

		public override bool CanResetValue(object component) {
			return false;
		}

		public override void ResetValue(object component) {
		}

		public override bool ShouldSerializeValue(object component) {
			return false;
		}

		public override Type ComponentType {
			get {
				return typeof(RowStructure);
			}
		}

		public override Type PropertyType {
			get {
				return typeof(string);
			}
		}

		public override object GetValue(object component) {
			return ((RowStructure)component).Node.Attributes[name].Value;
		}
	}

	public struct RowStructure : ICustomTypeDescriptor {
		private XmlNode node;

		public RowStructure(XmlNode node) {
			this.node = node;
		}

		public XmlNode Node {
			get {
				return node;
			}
		}

		// ICustomTypeDescriptor interface
		public AttributeCollection GetAttributes() {
			return AttributeCollection.Empty;
		}

		public string GetClassName() {
			return null;
		}

		public string GetComponentName() {
			return null;
		}

		public TypeConverter GetConverter() {
			return null;
		}
		
		public object GetEditor(Type t) {
			return null;
		}

		public EventDescriptor GetDefaultEvent() {
			return null;
		}

		public EventDescriptorCollection GetEvents(Attribute[] a) {
			return GetEvents();
		}

		public EventDescriptorCollection GetEvents() {
			return EventDescriptorCollection.Empty;
		}

		public object GetPropertyOwner(PropertyDescriptor pd) {
			return null;
		}

		public PropertyDescriptor GetDefaultProperty() {
			return null;
		}

		public PropertyDescriptorCollection GetProperties(Attribute[] a) {
			return GetProperties();
		}

		public PropertyDescriptorCollection GetProperties() {
			PropertyDescriptor[] pd = new PropertyDescriptor[node.Attributes.Count];
			for (int i = 0; i < node.Attributes.Count; i++)
				pd[i] = new ColumnStructure(node.Attributes[i].Name);
			return new PropertyDescriptorCollection(pd);
		}
	}

	public struct SmartProvider : IEnumerable, IEnumerator {
		private XmlNode root;
		private int current;

		public SmartProvider(XmlDocument document) {
			root = document.DocumentElement;
			current = -1; // Reset()
		}

		// IEnumerable interface
		public IEnumerator GetEnumerator() {
			return this;
		}

		// IEnumerator interface
		public void Reset() {
			current = -1;
		}

		public bool MoveNext() {
			current++;
			return current < root.ChildNodes.Count;
		}
        public string ElementTableValue2Dim(int nodo, int valor) {
            return root.ChildNodes.Item(nodo).Attributes[valor].Value.ToString();
        }
        
        public int RecordCount() {
            return root.ChildNodes.Count;
        }
        public int AttributeCount()
        {
            if (root.ChildNodes.Count > 0 && root.ChildNodes.Item(0).Attributes.Count > 0)
                return root.ChildNodes.Item(0).Attributes.Count;
            else return 0;
        }
		public object Current {
			get {
				try {
					XmlNode node = root.ChildNodes[current];
					return new RowStructure(node);
				} catch(Exception) {
				}
				throw new InvalidOperationException();
			}
		}
	}

